home *** CD-ROM | disk | FTP | other *** search
- /*
- Authors: David Nishimoto and Thomas Lee Young
- Website: http://www.listensoftware.com
- Program: Cube Drop
- */
-
- #include "stdafx.h"
- #include "MainFrm.h"
- #include "3DFont.h"
-
- #include "3DFontDoc.h"
- #include "3DFontView.h"
-
- #include <time.h>
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- #include "canvas.h"
- #include "matrix.h"
- #include "camera.h"
-
- Canvas cvs(0,0,640,480,"Cube Space Score: 0");
- Camera cam(PI/2.0, 12.0f, 90.0f);
- Matrix mat;
- BOOL gameOver = FALSE;
- BOOL pause = FALSE;
- BOOL autoRotate = FALSE;
- BOOL timersSet = FALSE;
-
- /////////////////////////////////////////////////////////////////////////////
- // CMy3DFontView
-
- IMPLEMENT_DYNCREATE(CMy3DFontView, CView)
-
- BEGIN_MESSAGE_MAP(CMy3DFontView, CView)
- //{{AFX_MSG_MAP(CMy3DFontView)
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_WM_SIZE()
- ON_WM_TIMER()
- ON_WM_KEYDOWN()
- ON_COMMAND(ID_VIEW_AUTO, OnViewAuto)
- ON_COMMAND(ID_VIEW_DEFAULT, OnViewDefault)
- ON_COMMAND(ID_FILE_PAUSE, OnFilePause)
- ON_COMMAND(ID_FILE_NEW, OnFileNew)
- ON_COMMAND(ID_FILE_RESUME, OnFileResume)
- ON_COMMAND(ID_VIEW_AUTOSTOP, OnViewAutostop)
- //}}AFX_MSG_MAP
- // Standard printing commands
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMy3DFontView construction/destruction
-
- CMy3DFontView::CMy3DFontView()
- {
- // TODO: add construction code here
- m_shininess = 10.0f;
- m_Lightposition[0] = 100.0f;
- m_Lightposition[1] = 0.0f;
- m_Lightposition[2] = 0.0f;
- m_Lightposition[3] = 0.0f;
- m_Lightspecular[0] = 0.3f;
- m_Lightspecular[1] = 0.3f;
- m_Lightspecular[2] = 0.1f;
- m_Lightspecular[3] = 1.0f;
- m_Lightdiffuse[0] = 0.1f;
- m_Lightdiffuse[1] = 0.1f;
- m_Lightdiffuse[2] = 0.1f;
- m_Lightdiffuse[3] = 1.0f;
- m_Lightambient[0] = 0.3f;
- m_Lightambient[1] = 0.3f;
- m_Lightambient[2] = 0.3f;
- m_Lightambient[3] = 1.0f;
-
- srand( (unsigned)time(NULL) );
-
- }
-
- CMy3DFontView::~CMy3DFontView()
- {
- }
-
- BOOL CMy3DFontView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CView::PreCreateWindow(cs);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMy3DFontView drawing
-
- void CMy3DFontView::OnDraw(CDC* pDC)
- {
- CMy3DFontDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
-
- // TODO: add draw code for native data here
- CPalette* oldPalette;
-
- //Set logic palette
- oldPalette = m_pDC->SelectPalette(&m_Palette, FALSE);
- m_pDC->RealizePalette();
-
- wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
- //DrawGLFont();
- drawWithOpenGL();
- SwapBuffers(m_pDC->GetSafeHdc());
- wglMakeCurrent(m_pDC->GetSafeHdc(), NULL);
- m_pDC->SelectPalette(oldPalette, FALSE);
- }
- void CMy3DFontView::drawWithOpenGL()
- {
- CMainFrame* pFrame;
- char scorePhrase[200];
-
- glShadeModel(GL_SMOOTH);
- glEnable(GL_DEPTH_TEST);
-
- //clear color buffer
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- //set light model
- glLightfv(GL_LIGHT0, GL_AMBIENT, m_Lightambient);//set light ambient
- glLightfv(GL_LIGHT0, GL_DIFFUSE, m_Lightdiffuse);//set light specular
- glLightfv(GL_LIGHT0, GL_SPECULAR, m_Lightspecular);//set light specular
- glLightfv(GL_LIGHT0, GL_POSITION, m_Lightposition);//set light position
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- if(autoRotate)
- autoRotateDisplay();
-
- gluLookAt(cam.getX(), cam.getY(), cam.getZ(),
- 12.0f, 0.0f, -12.0f,
- 0.0f, 1.0f, 0.0f);
-
- glPushMatrix();
- glTranslatef(-10.0f, -3.0f, 10.0f);
- m_FontX.GLDrawText();
- glPopMatrix();
-
- drawCubes();
-
-
- sprintf(scorePhrase,"(space)drop(Cam 1/2)In/Out(Cam 3/4)Rot(Cam 5/6)Up/Dn Score: %i", mat.score);
-
- VERIFY( pFrame = (CMainFrame*)GetParentFrame() );
-
- if(gameOver)
- {
- sprintf(scorePhrase,"Game Over Score %i",mat.score);
- pFrame->SetWindowText(scorePhrase);
- }
- else
- {
- pFrame->SetWindowText(scorePhrase);
- }
-
-
- //draw the game grid
- glRGB(0,255,0);
- drawGrid();
-
- }
- void CMy3DFontView::drawCubes()
- {
- int color;
-
- //position the cube in the matrix position given by x, y, and z
- GLfloat w, h, d;
-
- w = h = d = 5.0; //cube is 5 x 5 x 5
- for(int i = 0; i<MAT_WIDTH; ++i)
- for(int j = 0; j<MAT_HEIGHT; ++j)
- for(int k = 0; k<MAT_DEPTH; ++k)
- if(color = mat.isSet(i,j,k))
- {
- switch(color)
- {
- case 1: cvs.setAmbientMaterialColor(1,0,0,1);
- break;
- case 2: cvs.setAmbientMaterialColor(0,1,0,1);
- break;
- case 3: cvs.setAmbientMaterialColor(0,0,1,1);
- break;
- case 4: cvs.setAmbientMaterialColor(1,1,0,1);
- break;
- case 5: cvs.setAmbientMaterialColor(0,1,1,1);
- break;
- case 6: cvs.setAmbientMaterialColor(1,0,1,1);
- break;
- }
- glPushMatrix();
- glTranslatef(2.5+i*w, 2.5+j*h, -2.5 - k*h);
- cvs.Box(w,h,d);
- glPopMatrix();
- }
-
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMy3DFontView diagnostics
-
- #ifdef _DEBUG
- void CMy3DFontView::AssertValid() const
- {
- CView::AssertValid();
- }
-
- void CMy3DFontView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
-
- CMy3DFontDoc* CMy3DFontView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy3DFontDoc)));
- return (CMy3DFontDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CMy3DFontView message handlers
-
- int CMy3DFontView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- // TODO: Add your specialized creation code here
- PIXELFORMATDESCRIPTOR pfd =
- {
- sizeof(PIXELFORMATDESCRIPTOR),
- 1,
- PFD_DRAW_TO_WINDOW|
- PFD_SUPPORT_OPENGL|
- PFD_DOUBLEBUFFER,
- PFD_TYPE_RGBA,
- 24,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,0,
- 32,
- 0,0,
- PFD_MAIN_PLANE,
- 0,
- 0,0,0
- };
-
- m_pDC = new CClientDC(this);
- int pixelFormat =
- ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);
- BOOL success =
- SetPixelFormat(m_pDC->GetSafeHdc(), pixelFormat, &pfd);
-
- DescribePixelFormat(m_pDC->GetSafeHdc(), pixelFormat,
- sizeof(pfd), &pfd);
-
- if(pfd.dwFlags & PFD_NEED_PALETTE)
- InitPalette();
-
- m_hRC = wglCreateContext(m_pDC->GetSafeHdc());
-
- m_FontX.SetXOffset(0.0f);
- m_FontX.SetYOffset(0.1f);
- m_FontX.SetXScale(6.4f);
- m_FontX.SetYScale(6.3f);
- m_FontX.SetZScale(3.8f);
- m_FontX.SetXRotate(-90.0f);
- m_FontX.SetYRotate(0.0f);
- m_FontX.SetZRotate(0.0f);
-
- InitFontColor();
-
- wglMakeCurrent(m_pDC->m_hDC,m_hRC);
- m_FontX.CreateFont(m_pDC, "Arial Black");
- m_FontX.SetText("Cube Drop");
-
- wglMakeCurrent(NULL,NULL);
-
- setSpeed();
-
- startGame();
-
- return 0;
- }
-
- void CMy3DFontView::OnDestroy()
- {
- CView::OnDestroy();
-
- // TODO: Add your message handler code here
- wglDeleteContext(m_hRC);
- m_Palette.DeleteObject();
- ReleaseDC(m_pDC);
- }
-
- void CMy3DFontView::OnSize(UINT nType, int cx, int cy)
- {
- GLdouble aspect;
- CView::OnSize(nType, cx, cy);
-
- if (cy==0)
- aspect = (GLdouble)cx;
- else
- aspect = (GLdouble)cx/(GLdouble)cy;
-
- CClientDC clientDC(this);
- wglMakeCurrent(clientDC.m_hDC, m_hRC);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 7.0);
- //glViewport(0, 0, cx, cy);
-
- setPerspectiveWindow(60,aspect,5,4000);
- setViewport(0,0,cx,cy);
-
- wglMakeCurrent(NULL, NULL);
- }
-
-
- void CMy3DFontView::InitPalette(void)
- {
- PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
- LOGPALETTE *pPal; // Pointer to memory for logical palette
- int PixelFormat; // Pixel format index
- int paletteSize; // Number of entries in palette
- BYTE RedMask; // Range for each color entry (7,7,and 3)
- BYTE GreenMask;
- BYTE BlueMask;
- HDC hDC = GetDC()->GetSafeHdc(); //the context device
-
- // Get the pixel format index and retrieve the pixel format description
- PixelFormat = GetPixelFormat(hDC);
- DescribePixelFormat(hDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
-
-
- // Check whether the pixel format and the pixel type
- if (!(pfd.dwFlags & PFD_NEED_PALETTE ||
- pfd.iPixelType == PFD_TYPE_COLORINDEX))
- return;
-
- // Get the number of entries in palette. 256 colors for 8 bits
- paletteSize = 1 << pfd.cColorBits;
-
- // Allocate for the logical palette
- pPal = (LOGPALETTE*)
- malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY));
-
-
- // Fill the logical palette header information
- pPal->palVersion = 0x300; //support Windows3.0
- pPal->palNumEntries = paletteSize; //number of colors entries
-
- // Set the 1st entries of logical palette with the current system palette
- (void) GetSystemPaletteEntries(hDC, 0, paletteSize, &pPal->palPalEntry[0]);
-
- //Set the RGB mask
- RedMask = (1 << pfd.cRedBits) - 1;
- GreenMask = (1 << pfd.cGreenBits) - 1;
- BlueMask = (1 << pfd.cBlueBits) - 1;
-
- //Set all entries of the logical palette
- for (int i=0; i<paletteSize; ++i)
- {
- pPal->palPalEntry[i].peRed =
- (((i >> pfd.cRedShift) & RedMask) * 255) / RedMask;
- pPal->palPalEntry[i].peGreen =
- (((i >> pfd.cGreenShift) & GreenMask) * 255) / GreenMask;
- pPal->palPalEntry[i].peBlue =
- (((i >> pfd.cBlueShift) & BlueMask) * 255) / BlueMask;
- pPal->palPalEntry[i].peFlags = 0;
- }
-
- //Create the palette
- m_Palette.CreatePalette(pPal);
-
- //Free the memory allocated for the logical palette
- free(pPal);
- }
-
-
- void CMy3DFontView::DrawGLFont(void)
- {
- glShadeModel(GL_SMOOTH);
- glEnable(GL_DEPTH_TEST);
-
- //clear color buffer
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- //set light model
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- glTranslatef(-3.0f, -5.0f, -60.0f);
-
- m_FontX.GLDrawText();
-
- glFlush();
-
- }
-
-
- void CMy3DFontView::InitFontColor()
- {
- float fv[4];
-
-
- fv[0] = 0.1;
- fv[1] = 0.1;
- fv[2] = 0.1;
- fv[3] = 0.1;
- m_FontX.SetEmission(fv);
-
- fv[0] = 0.9;
- fv[1] = 0.9;
- fv[2] = 0.9;
- fv[3] = 1.0;
- m_FontX.SetSpecular(fv);
-
- fv[0] = 0.1;
- fv[1] = 0.1;
- fv[2] = 1.0;
- fv[3] = 1.0;
- m_FontX.SetAmbient(fv);
-
- fv[0] = 0.1;
- fv[1] = 0.1;
- fv[2] = 0.1;
- fv[3] = 1.0;
- m_FontX.SetDiffuse(fv);
-
- m_FontX.SetShininess(0.1f);
-
- }
-
-
- void CMy3DFontView::setPerspectiveWindow(GLdouble fovy, GLdouble aspect,GLdouble zNear,GLdouble zFar)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(fovy, aspect,zNear, zFar);
- }
- void CMy3DFontView::setViewport(GLint iX, GLint iY, GLsizei iWidth, GLsizei iHeight)
- {
- glViewport(iX,iY,iWidth,iHeight);
- }
- void CMy3DFontView::setDepthBuffer()
- {
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glFrontFace(GL_CCW);
- }
- void CMy3DFontView::startGame()
- {
- gameOver = FALSE;
- autoRotate = FALSE;
- cam.SetCamera(PI/2.0, 14.0f, 90.0f);
- mat.SetScore(0);
- mat.SetLevel(0);
- //mat.SetTitleBar();
- mat.ZeroMatrix();
- mat.set(3,10,3);
-
- //prevent two sets of timers from running if the game is reset
- if(!timersSet)
- {
- //glutTimerFunc(delayTime,TimerFunction, delayTime);
- //glutTimerFunc(10000, IncreaseSpeed, 10000);
- timersSet = TRUE;
- }
- }
-
-
- void CMy3DFontView::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
-
- BOOL successful;
-
- if(pause)
- {
- KillTimer(1);
- }
- else
- {
- if(autoRotate)
- {
- SetTimer(1,5,0);
- }
- else
- {
- setSpeed();
- successful = mat.moveDown();
- if(!successful)
- {
- gameOver = TRUE;
- autoRotate = TRUE;
- }
- }
- //printf("Delay Time: %i\n", delayTime);
- }
-
-
- Invalidate(FALSE);
-
- CView::OnTimer(nIDEvent);
- }
-
- void CMy3DFontView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
-
- if (nChar==37)
- {
- if(!pause) mat.moveLeft();
- }
- else if(nChar==39)
- {
- if(!pause) mat.moveRight();
- }
-
- else if(nChar==38)
- {
- if(!pause) mat.moveIn();
- }
- else if(nChar==40)
- {
- if(!pause) mat.moveOut();
- }
- else if(nChar==32)
- {
- mat.drop();
- }
- //1
- else if(nChar==49)
- {
- cam.Out();
- }
- //2
- else if(nChar==50)
- {
- cam.In();
- }
- //3
- else if(nChar==51)
- {
- cam.RotateC();
- }
- //4
- else if(nChar==52)
- {
- cam.RotateCC();
- }
- //5
- else if(nChar==53)
- {
- cam.Up();
- }
- //6
- else if(nChar==54)
- {
- cam.Down();
- }
- //7
- else if(nChar==55)
- {
- }
- //8
- else if (nChar==56)
- {
-
- }
-
- Invalidate(FALSE);
- CView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CMy3DFontView::drawGrid()
- {
- int r,c,d = 0;
- int nStep = 5;
- int width = cvs.gameWidth;
- int depth = cvs.gameDepth;
- int height = cvs.gameHeight;
-
-
-
- glPushAttrib(GL_LIGHTING_BIT);
- glDisable(GL_LIGHTING);
-
- glPushMatrix();
-
- glBegin(GL_LINES);
-
- //draw transverse grid lines
- for(r = 0; r <= width; r += nStep)
- {
- glVertex3d(r, height, 0);
- glVertex3d(r, 0, 0);
-
- // for(d = 0; d <= height; d += nStep)
- {
- glVertex3d(r, d, 0);
- glVertex3d(r, d, -depth);
- }
-
- glVertex3d(r, 0, -depth);
- glVertex3d(r, height, -depth);
- }
-
- //draw lateral grid lines
- for(c = 0; c <= depth; c += nStep)
- {
- glVertex3d(0, height, -c);
- glVertex3d(0, 0, -c);
-
- // for(d = 0; d <= height; d += nStep)
- {
- glVertex3d(0, d, -c);
- glVertex3d(width, d, -c);
- }
-
- glVertex3d(width, 0, -c);
- glVertex3d(width, height, -c);
- }
-
- //draw grid lines up the y axis
- for(d=0; d<=height; d+=nStep)
- {
- glVertex3d(0,d,0);
- glVertex3d(width,d,0);
-
- glVertex3d(width,d,0);
- glVertex3d(width,d,-depth);
-
- glVertex3d(width,d,-depth);
- glVertex3d(0,d,-depth);
-
- glVertex3d(0,d,-depth);
- glVertex3d(0,d,0);
- }
-
- glEnd();
-
- glPopMatrix();
- glPopAttrib();
- glEnable(GL_LIGHTING);
- }
-
- void CMy3DFontView::OnViewAuto()
- {
- // TODO: Add your command handler code here
- autoRotate=TRUE;
- }
- void CMy3DFontView::autoRotateDisplay()
- {
- static int movement = 0;
- static int count = 0;
-
- //only change the direction of change
- //on even multiples of 125
- if(count%125 == 0)
- {
- movement = rand()%6+1;
- }
-
- //only change the view when count is an even multiple of 2
- //if the graphics card is very slow, decrease this
- if(count%1 == 0)
- {
- switch(movement)
- {
- case 1: cam.Up(); break;
- case 2: cam.Down(); break;
- case 3: cam.RotateCC(); break;
- case 4: cam.RotateC(); break;
- case 5: cam.In(); break;
- case 6: cam.Out(); break;
- }
- }
-
- ++count;
-
- }
-
- void CMy3DFontView::OnViewDefault()
- {
- // TODO: Add your command handler code here
- autoRotate=FALSE;
- pause=FALSE;
- cam.SetCamera(PI/2.0, 12.0f, 90.0f);
- }
-
- void CMy3DFontView::OnFilePause()
- {
- // TODO: Add your command handler code here
- pause=TRUE;
- }
- void CMy3DFontView::setSpeed()
- {
- SetTimer(1,1000-mat.GetLevel(),0);
- }
-
- void CMy3DFontView::OnFileNew()
- {
- // TODO: Add your command handler code here
- startGame();
- }
-
- void CMy3DFontView::OnFileResume()
- {
- // TODO: Add your command handler code here
-
- pause=FALSE;
- setSpeed();
- }
-
- void CMy3DFontView::OnViewAutostop()
- {
- // TODO: Add your command handler code here
- autoRotate=FALSE;
- }
-